package com.example.meituan.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.meituan.activity.bean.ShopCarBean;
import com.example.meituan.db.DBUntil;

import java.util.ArrayList;
import java.util.List;

public class ShopCarDao {
    private static SQLiteDatabase db= DBUntil.con;



    public static int insert(ShopCarBean shopCar) {
        ContentValues values = new ContentValues();
        values.put("s_id", shopCar.getsId());
        values.put("s_user_id", shopCar.getsUserId());
        values.put("s_shop_id", shopCar.getsShopId());
        values.put("s_time", shopCar.getsTime());
        values.put("s_type_id",shopCar.getsType());
        try{
            db.insert("d_shop_car", null, values);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }


    public int update(ShopCarBean shopCar) {
        ContentValues values = new ContentValues();
        values.put("s_user_id", shopCar.getsUserId());
        values.put("s_shop_id", shopCar.getsShopId());
        values.put("s_time", shopCar.getsTime());
        values.put("s_type_id",shopCar.getsType());
        return db.update("d_shop_car", values, "s_id=?", new String[]{shopCar.getsId()});
    }


    public static int delete(String sId) {
        try {
            db.delete("d_shop_car", "s_id=?", new String[]{sId});
            return 1;
        }catch (Exception e){
            return 0;
        }
    }


    public ShopCarBean findById(String sId) {
        Cursor cursor = db.query("d_shop_car", null, "s_id=?", new String[]{sId}, null, null, null);
        ShopCarBean shopCar = null;
        if (cursor.moveToFirst()) {
            shopCar = buildShopCarFromCursor(cursor);
        }
        cursor.close();
        return shopCar;
    }

    public static List<ShopCarBean> findAllByShopId(String shopId) {
        List<ShopCarBean> shopCars = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT * FROM d_shop_car WHERE s_shop_id=?", new String[]{shopId});
        while (cursor.moveToNext()) {
            shopCars.add(buildShopCarFromCursor(cursor));
        }
        cursor.close();
        return shopCars;
    }

    public static List<ShopCarBean> findAllByUserIdName(String userId, String query) {
        List<ShopCarBean> shopCars = new ArrayList<>();
        String[] selectionArgs;

        String sql = "SELECT dsc.* FROM d_shop_car AS dsc " +
                "JOIN d_shop AS ds ON dsc.s_shop_id = ds.id " +
                "WHERE dsc.s_user_id = ? AND (ds.s_type LIKE ? OR ds.s_name LIKE ?)";

        selectionArgs = new String[]{userId, "%" + query + "%", "%" + query + "%"};

        Cursor cursor = null;
        try {
            cursor = db.rawQuery(sql, selectionArgs);
            while (cursor.moveToNext()) {
                ShopCarBean shopCarBean = buildShopCarFromCursor(cursor);
                shopCars.add(shopCarBean);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return shopCars;
    }

    public List<ShopCarBean> findAll() {
        List<ShopCarBean> shopCars = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT * FROM d_shop_car", null);
        while (cursor.moveToNext()) {
            shopCars.add(buildShopCarFromCursor(cursor));
        }
        cursor.close();
        return shopCars;
    }


    public static List<ShopCarBean> findByUserId(String sUserId) {
        List<ShopCarBean> shopCars = new ArrayList<>();

        Cursor cursor = db.query(
                "d_shop_car",
                null,
                "s_user_id=?",
                new String[]{sUserId},
                null,
                null,
                "s_time DESC"
        );

        try {
            while (cursor.moveToNext()) {
                shopCars.add(buildShopCarFromCursor(cursor));
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return shopCars;
    }

    public static int CountByShopId(String sShopId) {
        int count = 0;
        try (Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM d_shop_car WHERE s_shop_id=?", new String[]{sShopId})) {
            if (cursor.moveToFirst()) {
                count = cursor.getInt(0);
            }
        } catch (Exception e) {
           return 0;
        }
        return count;
    }

    public static int CountByUserId(String sUserId) {
        int count = 0;
        try (Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM d_shop_car WHERE s_user_id=?", new String[]{sUserId})) {
            if (cursor.moveToFirst()) {
                count = cursor.getInt(0);
            }
        } catch (Exception e) {
            return 0;
        }
        return count;
    }

    private static ShopCarBean buildShopCarFromCursor(Cursor cursor) {
        ShopCarBean shopCar = new ShopCarBean(
                cursor.getString(cursor.getColumnIndexOrThrow("s_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_user_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_shop_id")),
                cursor.getLong(cursor.getColumnIndexOrThrow("s_time")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_type_id"))
        );
        return shopCar;
    }
}
